package com.bing.bingPro.controller.datebase;


import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.controller.dto.MgDamTableRelationDTO;
import com.bing.bingPro.entity.TOltSgcc;
import com.bing.bingPro.entity.TSgccPowerCutNoticeAll;
import com.bing.bingPro.service.MgDamTableRelationService;
import com.bing.bingPro.service.TOltSgccService;
import com.bing.bingPro.service.TSgccPowerCutNoticeAllService;
import com.bing.bingPro.service.TSgccPowerCutNoticeService;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.ExcelUtils;
import com.bing.bingPro.util.WebExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据资产数据表关系配置 控制层
 */
@RestController
@RequestMapping("/mgDamTableRelation")
public class MgDamTableRelationController {

    private final MgDamTableRelationService mgDamTableRelationService;
    @Resource
    private TSgccPowerCutNoticeService tSgccPowerCutNoticeService;
    @Resource
    private TOltSgccService tOltSgccService;

    @Resource
    private TSgccPowerCutNoticeAllService tSgccPowerCutNoticeAllService;

    public MgDamTableRelationController(MgDamTableRelationService mgDamTableRelationService) {
        this.mgDamTableRelationService = mgDamTableRelationService;
    }

    /**
     * 数据血缘分析-查询所属关系图
     *
     * @param infoDTO 数据资产数据表关系ID
     * @return 数据表列表详情
     */
    @PostMapping("list")
    public BaseResult<Map<String, Object>> list(@RequestBody MgDamTableRelationDTO infoDTO) {
        return new BaseResult<>(mgDamTableRelationService.list(infoDTO));
    }


    @PostMapping("list2")
    public BaseResult<Map<String, Object>> list2(@RequestBody MgDamTableRelationDTO infoDTO) {
        return new BaseResult<>(mgDamTableRelationService.list2(infoDTO));
    }

    @PostMapping("upload")
    public BaseResult<String> upload(@RequestPart MultipartFile file) throws Exception {
        List<TOltSgcc> list = ExcelUtils.readMultipartFile(file, TOltSgcc.class);
        list.forEach(tSgccPowerCutNotice -> {
            tOltSgccService.getBaseMapper().insert(tSgccPowerCutNotice);
        });
        return new BaseResult<>("成功");
    }


    @PostMapping("select")
    public void select(HttpServletResponse response, @RequestBody MgDamTableRelationDTO infoDTO) {
        try {
            String time = infoDTO.getTime();
            System.out.println(time);
            //查询数据
            //List<TSgccPowerCutNoticeAll> list = tSgccPowerCutNoticeService.list(infoDTO);
            List<TSgccPowerCutNoticeAll> list = tSgccPowerCutNoticeAllService.list()
                    .stream()
                    .sorted(Comparator.comparing(TSgccPowerCutNoticeAll::getOltFaultStartTime))
                    .collect(Collectors.toList());
            list.forEach(tSgccPowerCutNoticeAll -> {
                if ("1".equals(tSgccPowerCutNoticeAll.getStats())) {
                    tSgccPowerCutNoticeAll.setStats("是");
                }
                if ("0".equals(tSgccPowerCutNoticeAll.getStats())) {
                    tSgccPowerCutNoticeAll.setStats("否");
                }
            });
            List<ExcelData> excelDataList = new ArrayList<>();
            ExcelData excelData = new ExcelData(TSgccPowerCutNoticeAll.class,
                    list);
            excelDataList.add(excelData);
            //写出到浏览器
            WebExcelUtil.download(response, "国家电网停电公告_" + infoDTO.getTime(), excelDataList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
